<template>
	<view class="addr-page">
		<van-address-list 
		v-model="chosenAddressId" 
		:list="list" 
		@add="onAdd" 
		@edit="onEdit" 
		@select="onselect"/>
	</view>
</template>

<script setup>
	import {
		ref, watch
	} from 'vue';
	import {
		showToast
	} from 'vant';
	import {
		useStore
	} from 'vuex'
	import {
		onLoad,onShow
	} from '@dcloudio/uni-app';
	import api from './../../lib/request.js'
	
	//地址
	const chosenAddressId = ref('1');
	const store = useStore()
	let id = ref(store.state.uid)
	const list = ref([]);
	//新增
	const onAdd = () => {
		uni.navigateTo({
			url: '/pages/edit/edit'
		})
	}
	//编辑地址
	const onEdit = (item, index) => {
		showToast('编辑地址'+index )
		uni.navigateTo({
				url:'/pages/edit/edit?id='+item.id
		})
	};
	
	const onselect = (item)=>{
		store.commit('update_addr',item)
		if(item){
			uni.reLaunch({
				url:'/pages/checkout/checkout'
			})
		}
	}
	
	//初始化
	onLoad(() => {
		api.get(`/address/findAll/${id.value}`).then(res=>{
			let {data} = res
			console.log(res);
			for(let k of data){
				let a = { id: k.id,name:k.name,
				tel:k.iphone,address:`${k.province}${k.city}${k.area}${k.detailedAddress}`}
				list.value.push(a)
			}
		})
	})
</script>

<style>

</style>